--
-- Author: ping.wang
-- Date: 2018-09-25 17:22:53
--
-- newCrystalPropView
--

local crystalNodeNum = 6 -- 水晶孔个数
local panelOriginHeight = 0 -- 初始全尺寸高
local distanceOfTop = 0 -- 距离上边界距离
local distanceOfJewel = 0 -- 水晶间距
local crystalNodeHeight = 0 -- 水晶Node高

local crystalPropRender = import(".newCrystalPropRender")

local ClassRef = class("newCrystalPropView",function()
    return ccui.Layout:create()
end)


function ClassRef:ctor()
    local function createModuleNode(uiPath, uiNodeName, parent)
        local node = cc.uiloader:load( uiPath )
        local moduleNode = node:getChildByName( uiNodeName )
        moduleNode:retain()
        moduleNode:removeFromParent()
        parent:addChild( moduleNode )
        moduleNode:release()
        return moduleNode
    end

    self.mUINode = createModuleNode( Res.Tips_Crystal, "Panel", self )
    panelOriginHeight = self.mUINode:getContentSize().height

    self:setContentSize( self.mUINode:getContentSize() )
    ccui.Helper:doLayout( self.mUINode )

    self._jewelNodeList = {}
    for i = 1, crystalNodeNum do
        local nodeTemp = crystalPropRender.create( self.mUINode:getChildByName( "jewel_"..i ) )
        nodeTemp:setData()
        table.insert( self._jewelNodeList, nodeTemp )
    end

    self._tlt = self.mUINode:getChildByName("tlt")

    local _,jewelNodeHeightTemp = self._jewelNodeList[1]:getContentSize2()
    crystalNodeHeight = jewelNodeHeightTemp
    distanceOfTop = panelOriginHeight - ( self._jewelNodeList[1]:getPositionY() + crystalNodeHeight )
    distanceOfJewel = self._jewelNodeList[1]:getPositionY() - ( self._jewelNodeList[3]:getPositionY() + crystalNodeHeight )
end

--传入水晶数据 和 插槽数量
function ClassRef:refreshContent( jewelSlot, rockSlotNum)
    for i = 1, crystalNodeNum do
        self._jewelNodeList[ i ]:setData()
    end

    -- 填充宝石属性
    for i = 1, rockSlotNum do
        if i <= #jewelSlot then
            self._jewelNodeList[ i ]:setData( jewelSlot[i] )
        else
            self._jewelNodeList[ i ]:setData( 0 )
        end
    end

    --刷新窗口高度
    local sizeWidth = self.mUINode:getContentSize2()
    local adjustedHeight = panelOriginHeight - (crystalNodeHeight + distanceOfJewel) * (math.ceil(crystalNodeNum/2) - math.ceil(rockSlotNum/2))

    --添加了一句话
    local needLv = tonumber(GD:getSystemPar("wingsEmbedLv",0))
    if UD:getLevel() >= needLv then
        adjustedHeight = adjustedHeight - 22
        self._tlt:setVisible(false)
    else
        self._tlt:setVisible(true)
        self._tlt:setString(string.format(L("wingsEmbedLvString"),needLv))
    end


    self.mUINode:setContentSize( cc.size( sizeWidth, adjustedHeight ) )

    -- 调整jewelNode PosY
    for i = 1, crystalNodeNum do
        local posYTemp = adjustedHeight - distanceOfTop - crystalNodeHeight * math.ceil(i/2) - distanceOfJewel*(math.ceil(i/2)-1)
        self._jewelNodeList[ i ]:setPositionY( posYTemp )
    end

    self:setContentSize( self.mUINode:getContentSize() )
    ccui.Helper:doLayout( self.mUINode )
end
return ClassRef
